package com.cqeec.netdisc0215.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.openstack4j.api.OSClient.OSClientV3;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cqeec.netdisc0215.model.User;
import com.cqeec.netdisc0215.util.OpenStackClientUtil;


/**
 * 文件夹控制器。
 * 
 * @author 兰文杰
 * 
 * @date 2020-11-03 15:15:00
 *
 */
@RestController
public class FolderController {
	@PostMapping("/folder/new")
	public Map<String, Object> newFolder(HttpSession session, String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		if (obj == null) {
			map.put("code", 1);
			map.put("message", "用户信息丢失！");
		} else {
			User user = (User) obj;
			try {
				OSClientV3 os = OpenStackClientUtil.getOSClient();
				os.objectStorage().containers().createPath(user.getLoginName(), name);
				map.put("code", 0);
				map.put("message", "创建文件夹成功！");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 2);
				map.put("message", "创建文件夹失败！");
			}
		}
		return map;
	}
}